/*****************************************************************************************
  Project   : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
  Authors   : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
  Date      : August 2025
  Output    : Table 2 -  subsample anlaysis
  Notes     : This file has options for:
            : type of transitions `1'
*****************************************************************************************/


clear
set more off

// Table 2 
// Subsamples

// Table parameterrs
	// outide options
	global rents   dPE_n dPE_u   
	
	// IVs
	global IV1 IV_n IV_u

	// groups
	local outgrp Men Women Young Old Low High  EF_young_low  EF_young_high EF_old_low EF_old_high

cap mat drop out
foreach grp in `outgrp' { 

	use ${wd}/reg_dataRRv3_${ind}_${area}_`grp'_case_`1'.dta, clear

	// 1980 values of union and union premium for each group
	sum union_density if year2==1, meanonly 
	local ud = r(mean)

	sum union_premium if year2==1, meanonly 
	local up = r(mean)

	// spcification
	ivreghdfe dwage dER   ($rents = $IV1  ) [aw=weight],a( controls  )  cluster($clustlevel) first  

	// collect ouput
	mat x = _b[dPE_n],_b[dPE_u], _se[dPE_n], _se[dPE_u],el(e(first),11,1),el(e(first),11,2), e(N),`ud', `up'
	mat  colname x = B1 B2 ses1 ses2 F1 F2  N U P
	mat  rowname x = `grp'
	mat out = nullmat(out) \ x

}


// create table
clear
svmat out, n(col)

gen t1 = B1 / ses1
gen t2 = B2 / ses2

gen str coef1 = string(B1,"%3.2f")
replace coef1 =  coef1 + "$^{**}$"  if abs(t1) >= 1.96
replace coef1 =  coef1 + "$^{*}$ "   if abs(t1) >=  1.65 & abs(t1) <1.96
replace coef1 =  coef1 + "      "   if abs(t1) <  1.65 

gen str coef2 = string(B2,"%3.2f")
replace coef2 =  coef2 + "$^{**}$"  if abs(t2) >= 1.96
replace coef2 =  coef2 + "$^{*}$ "  if abs(t2) >=  1.65 & abs(t2) <1.96
replace coef2 =  coef2 + "      "   if abs(t2) <  1.65 

gen str se1 = string(ses1,"%3.2f")
replace se1 = " (" + se1 + ")"

gen str se2 = string(ses2,"%3.2f")
replace se2 = " (" + se2 + ")" 

replace coef1 =  coef1 + se1
replace coef2 =  coef2 + se2
  
gen Fs1 = string(F1,"%3.2f")
gen Fs2 = string(F2,"%3.2f")

tostring N, replace

gen str tmp = string(U,"%3.2f")
drop U
rename tmp U

gen str tmp = string(P,"%3.2f")
drop P
rename tmp P

gen str group = ""
local i = 1

*if "`2'" == "EF" {
	foreach var in  Men Women "Age 20-35" "Age 36-55" "$ \leq $ HS" "$ >$ HS"  "\footnotesize{Men Young/Low skill}" "\footnotesize{Men Young/High Skill}" "\footnotesize{Men Old/Low Skill}" "\footnotesize{Men Old/High Skill}"   {
	replace group = "`var'" if _n == `i'
	local i = `i'+ 1
	}
*}


keep group coef1  coef2    N U P
order group  coef1  coef2      N U P

lab var group "Sample"
lab var coef1 " \ \ \  $ \gamma_1 $" 
lab var coef2 " \ \ \ $ \tilde{\gamma}_1 $"
lab var N "$ N $"
lab var U "Union Prop."
lab var P "Union Prem."

		 
texsave using "${tables}//Table_02.tex",  replace frag autonumber nofix width(15cm)  rowsep(.1cm) varlabels  ///
		headerlines(" & \multicolumn{2}{c}{Coefficient} & & \multicolumn{2}{c}{1980}" "\cmidrule{2-3} \cmidrule{5-6}\addlinespace[-2ex]")  ///
		footnote(\textbf{\textsc{Notes:}}  This table reports 2SLS estimates from specification \eqref{wagefull} on separate subsamples. Columns (1) and (2) report coefficient estimates and standard errors for each group; all first-stage $ p $-values for instrument relevance are below 0.03. Column (3) reports the number of observations. Columns (4) and (5) show each group's 1980 unionization rate and union wage premium respectively with the latter adjusted for worker characteristics. , width(15cm )   ) ///
		title(Subsample Analysis - Coefficient Estimates on Outside Options \label{tab:3:hetero})
